<?php
/**
 * [LupMisNotAllowedSync]
 * 本代码为系统自动生成代码，请根据自己业务进行修改;
 * 生成时间 2022-02-18 18:16:27;
 * 版权所有 2020-2022 lizhongwen，并保留所有权利;
 * 说明: 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；不允许对程序代码以任何形式任何目的的再发布;
 * 作者: 中文Lee;
 * 作者主页: http://www.lizhongwen.com;
 * 邮箱: 360811363@qq.com;
 * QQ: 360811363;
 */
namespace modules\sys\service\impl;

use application\core\Mapper;
use application\core\Result;
use application\core\SnowFlake;
use Exception;
use modules\sys\component\SetCacheComponent;
use modules\sys\mapper\RoleMapper;
use modules\sys\mapper\RolePageActionMapper;
use modules\sys\mapper\RoleTreeMapper;
use modules\sys\service\RoleService;

class RoleServiceImpl implements RoleService
{

    public function lists(array $data, array $pagePars): array
    {
        return Result::success(false,RoleMapper::lists($data, $pagePars));
    }

    public function add(array $data): array
    {
        //开启事务
        Mapper::beginTransaction();
        try {
            //角色的id
            $id = SnowFlake::instance()->nextId();
            $data['id'] = $id;
            $add = RoleMapper::add($data);
            if ($add == 0) {
                Mapper::rollBack();
                return Result::fail(false,[], "添加失败");
            }
            //添加菜单权限
            $treeIds = $data['tree_ids'];
            $pageIds = $data['page_ids'];

            if (!empty($treeIds)) {
                $treeIdsArr = explode(",", $treeIds);
                foreach ($treeIdsArr as $treeId) {
                    RoleTreeMapper::add(['id' => SnowFlake::instance()->nextId(), 'tree_id' => $treeId, 'role_id' => $id]);
                }
            }
            //添加页面以及操作功能权限
            if (!empty($pageIds)) {
                $pageIdsArr = json_decode($pageIds, true);
                foreach ($pageIdsArr as $pageId => $actionIdsArr) {
                    $actionIds = "";
                    if (!empty($actionIdsArr)) {
                        $actionIdsArr = array_unique($actionIdsArr);
                        $actionIds = implode(",", $actionIdsArr);
                    }
                    RolePageActionMapper::add(['id' => SnowFlake::instance()->nextId(), 'page_id' => $pageId, 'role_id' => $id, 'action_ids' => $actionIds]);
                }
            }
            //提交事务
            Mapper::commit();
            SetCacheComponent::role();
            return Result::success(true,[], "添加成功");
        } catch (Exception $e) {
            //出现异常回滚事务
            Mapper::rollBack();
            return Result::fail(true,[], "添加失败->启动事务");
        }
    }

    public function edit(array $data): array
    {
        //开启事务
        Mapper::beginTransaction();

        try {
            //角色的id
            $id = $data['id'];
            RoleMapper::edit($data);
            //先删除菜单权限
            RoleTreeMapper::delByRoleId(['role_id' => $id]);
            //先删除页面权限
            RolePageActionMapper::delByRoleId(['role_id' => $id]);

            //添加菜单权限
            $treeIds = $data['tree_ids'];
            $pageIds = $data['page_ids'];
            if (!empty($treeIds)) {
                $treeIdsArr = explode(",", $treeIds);
                foreach ($treeIdsArr as $treeId) {
                    RoleTreeMapper::add(['id' => SnowFlake::instance()->nextId(), 'tree_id' => $treeId, 'role_id' => $id]);
                }
            }
            //添加页面以及操作功能权限
            if (!empty($pageIds)) {
                $pageIdsArr = json_decode($pageIds, true);
                foreach ($pageIdsArr as $pageId => $actionIdsArr) {
                    $actionIds = "";
                    if (!empty($actionIdsArr)) {
                        $actionIdsArr = array_unique($actionIdsArr);
                        $actionIds = implode(",", $actionIdsArr);
                    }
                    RolePageActionMapper::add(['id' => SnowFlake::instance()->nextId(), 'page_id' => $pageId, 'role_id' => $id, 'action_ids' => $actionIds]);
                }
            }
            //提交事务
            Mapper::commit();
            SetCacheComponent::role();
            return Result::success(true,[], "修改成功");
        } catch (Exception $e) {
            //出现异常回滚事务
            Mapper::rollBack();
            return Result::fail(true,[], "修改失败->启动事务");
        }
    }

    public function query(array $data): array
    {
        return Result::success(false,RoleMapper::query($data));
    }

    function del(array $data): array
    {

        if ($data['ids'] <= 5) {
            return Result::fail(false,[], "系统角色不允许删除");
        }
        $del = RoleMapper::del(['id' => $data['ids']]);
        if ($del == 0) {
            return Result::fail(false,[], "删除失败");
        }
        //删除菜单权限
        RoleTreeMapper::delByRoleId(['role_id' => $data['ids']]);
        //删除页面权限
        RolePageActionMapper::delByRoleId(['role_id' => $data['ids']]);
        return Result::success(true,[], "删除成功");
    }

}

